package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitService rabbitService;


    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        QueryWrapper<PaymentInfo> paymentQueryWrapper = new QueryWrapper<>();
        paymentQueryWrapper.eq("order_id", orderInfo.getId());
        paymentQueryWrapper.eq("payment_type", paymentType);
        PaymentInfo paymentInfoExit = paymentInfoMapper.selectOne(paymentQueryWrapper);
        if (paymentInfoExit != null) return;

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());

        paymentInfoMapper.insert(paymentInfo);
    }


    @Override
    public void paySuccess(String outTradeNo, String paymentType, Map<String, String> paramsMap) {
        //  获取到paymentInfo 对象
        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, paymentType);
        if (paymentInfoQuery==null) return;

        try {
            //  设置更新的内容
            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(paramsMap.toString());

            //  更新数据：
            // paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);
            this.updatePaymentInfo(outTradeNo,paymentType,paymentInfo);
        } catch (Exception e) {
            e.printStackTrace();
            //  如果处理异常，则需要删除掉缓存的notify_id
            redisTemplate.delete(paramsMap.get("notify_id"));
        }

        //  发送消息.
        this.rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());
    }


   /* @Override
    public void paySuccess(String outTradeNo, String paymentType, Map<String, String> paramsMap) {
        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, paymentType);

        //判断
        if (paymentInfoQuery == null) return;

        try {
            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(paramsMap.toString());
            this.updatePaymentInfo(outTradeNo, paymentType, paymentInfo);

        } catch (Exception e) {
            e.printStackTrace();
            //  如果处理异常，则需要删除掉缓存的notify_id
            redisTemplate.delete(paramsMap.get("notify_id"));
        }

        //  发送消息.
        this.rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY, paymentInfoQuery.getOrderId());
    }*/

    @Override
    public void updatePaymentInfo(String outTradeNo, String paymentType, PaymentInfo paymentInfo) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type", paymentType);

        //更新订单信息
        paymentInfoMapper.update(paymentInfo, paymentInfoQueryWrapper);

    }


    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        //查询数据
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type", paymentType);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
        //判断
        if (paymentInfo == null) return null;
        return paymentInfo;
    }

    @Override
    public void closePayment(Long orderId) {
        //  第一个参数：要修改的内容，第二个参数：表示要更新的条件.
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());

        //更新条件
        UpdateWrapper<PaymentInfo> paymentInfoUpdateWrapper = new UpdateWrapper<>();
        paymentInfoUpdateWrapper.eq("order_id", orderId);
        paymentInfoMapper.update(paymentInfo, paymentInfoUpdateWrapper);
    }
}
